package com.wanhe.base.runner;


import android.util.Log;

import com.wanhe.base.BaseApplication;
import com.wanhe.base.util.ViewUtil;

import org.reactivestreams.Subscriber;

import javax.inject.Inject;

import io.reactivex.Flowable;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by yangchaoxun on 2016/11/15.
 */
public class Runner {
    Disposable disposable;
    @Inject
    public Runner(){

    }

//    public void execute(Observable observable, Observer userCaseSubscriber){
//        Log.d("Runer","execute");
//       observable.subscribeOn(Schedulers.from(new MainTaskExecutor()))
//                .observeOn(AndroidSchedulers.mainThread())
//                .unsubscribeOn(Schedulers.io())
//               .subscribe(userCaseSubscriber);
//    }


    public void execute(Flowable observable, Subscriber userCaseSubscriber){
       observable.subscribeOn(Schedulers.from(new MainTaskExecutor()))
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(userCaseSubscriber);
    }


    public void execute(Observable observable, Consumer next){
        disposable =observable.subscribeOn(Schedulers.from(new MainTaskExecutor()))
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(next,getError());
    }

    public void execute(Observable observable, Consumer next,Consumer<? super Throwable> onError){
        disposable =observable.subscribeOn(Schedulers.from(new MainTaskExecutor()))
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(next,onError);
    }

    public void resendMsg( Consumer next){
        Observable observable = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(@NonNull ObservableEmitter e) throws Exception {

            }
        });
        disposable =observable.subscribeOn(Schedulers.from(new MainTaskExecutor()))
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(next);
    }
    public void unsubscribe(){
        if(disposable!=null){
            if(!disposable.isDisposed()){
                disposable.dispose();
            }
        }
    }

    public Consumer getError(){
       return   new Consumer<Exception>(){

           @Override
           public void accept(@NonNull Exception e) throws Exception {
               if(e!=null){
                   ViewUtil.LongToast(BaseApplication.context,e.getMessage());
               }else{
                   ViewUtil.ShortToast(BaseApplication.context,"服务器和网络,心情有点差");
               }
               ViewUtil.dismissLoadingDialog();
           }
       };
    }
}
